package cn.jantd.modules.system.service.impl;

import cn.jantd.modules.system.entity.*;
import cn.jantd.modules.system.mapper.SysTeamUserMapper;
import cn.jantd.modules.system.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @Author xiagf
 * @date 2019-07-04
 */
@Service
public class SysTeamUserServiceImpl extends ServiceImpl<SysTeamUserMapper, SysTeamUser> implements ISysTeamUserService {

    @Autowired
    ISysTeamService teamService;
    @Autowired
    ISysUserService userService;
    @Autowired
    private SysTeamUserMapper tuserMapper;

    /**
     * queryUserRole调用的方法
     *
     * @param userList
     * @param userId
     * @return
     */
    private String searchByUserId(List<SysUser> userList, String userId) {
        while (true) {
            for (SysUser user : userList) {
                if (userId.equals(user.getId())) {
                    return user.getUsername();
                }
            }
        }
    }

    /**
     * 查询所有团队对应的角色信息
     */
    @Override
    public Map<String, String> queryTeamUser() {
        List<SysTeamUser> uUserList = this.list();
        List<SysTeam> teamList = teamService.list();
        List<SysUser> userList = userService.list();
        Map<String, String> map = new IdentityHashMap<>();
        String teamId = "";
        String userId = "";
        String username = "";
        if (!uUserList.isEmpty()) {
            for (SysTeamUser uUser : uUserList) {
                userId = uUser.getUserId();
                for (SysTeam team : teamList) {
                    teamId = team.getId();
                    if (uUser.getTeamId().equals(teamId)) {
                        username = this.searchByUserId(userList, userId);
                        map.put(teamId, username);
                    }
                }
            }
            return map;
        }
        return map;
    }

    @Override
    public List<SysTeamUser> getByTeamId(String teamId) {
        List list = tuserMapper.getByTeamId(teamId);
        return list;
    }
}
